package com.ciwong.epaper.modules.epaper.ui;

import android.util.Log;
import android.view.View;
import android.widget.GridView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.adapter.BookAdapter;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.modules.epaper.bean.UserEpage;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.DeviceUtils;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 书籍编辑界面
 *
 * @author sugy
 * @version ciwong v.1.0 2016/4/12 17:10
 * @since ciwong v.1.0
 */
public class EditBookActivity extends BaseActivity {


    private GridView gvBook;

    private int width, height;
    private BookAdapter bookAdapter;

    private TitleBar edit_book_tittle;

    private  List<EpaperInfo> epaperInfoList=new ArrayList<>();
    private  float bookWidth;

    private  float bookHeight;

    @Override
    protected int setView() {
        return R.layout.activity_book_edit;
    }

    @Override
    protected void findViews() {
        gvBook = (GridView) findViewById(R.id.book_gv);
        edit_book_tittle = (TitleBar) findViewById(R.id.edit_book_tittle);
    }

    @Override
    protected void initEvent() {

//        setRightBtnListener(new ViewOnClickListener() {
//            @Override
//            public void avertRepeatOnClick(View v) {
//                finish();
//            }
//        });

    }

    @Override
    protected void init() {
//        setTitleText("删除书籍");
//        setRightBtnText(R.string.over);
//        showRightBtn();

        hideTitleBar();
        edit_book_tittle.setTitle("管理书籍");
        edit_book_tittle.hideBackBar();


        findViewById(R.id.btn_complete).setOnClickListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View v) {
                finish();
            }
        });


        width = getResources().getDisplayMetrics().widthPixels;
        height = getResources().getDisplayMetrics().heightPixels - DeviceUtils.dip2px(92);
//        float gHeight = height * 0.3f;
        float gWidth = width*0.32f;
         bookWidth = gWidth;
         bookHeight = gWidth * 210 / 150;

//        bookAdapter = new BookAdapter(this, epaperInfoList,(int)bookWidth,(int)bookHeight);
//        gvBook.setAdapter(bookAdapter);
    }

    @Override
    protected void loadData() {
        getBookcaseFromNet( );
    }


    /**
     * 获取网络书籍列表
     */
    private void getBookcaseFromNet( ) {

        if (null == EApplication.getInstance().getUserInfoBase()) {
            return;
        }

        EpaperDao.getInstance().getEpageBookcaseAll(EApplication.BRAND_ID ,new BaseExtCallBack(this,EApplication.getInstance().getUserInfoBase().getUserId()+"") {


            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                CWLog.e("TAT", "【EpaperFragment】    书籍列表获取失败1");
                showToastError(errorCode,data);
                hideCricleProgress();
            }

            @Override
            public void failed(Object data) {
                super.failed(data);
                CWLog.e("TAT", "【EpaperFragment】    书籍列表获取失败2");
                hideCricleProgress( );
                if (!NetworkUtils.isOnline()) {
                    Log.d(TAG, "######无网络#####");
                    ToastUtil.INSTANCE.toastCenterNoNetError();

                }


            }

            @Override
            public void success(Object data) {
                CWLog.e("TAT", "【EpaperFragment】    书籍列表获取成功");
                UserEpage userEpage = (UserEpage) data;
                List<EpaperInfo> netList=new ArrayList<EpaperInfo>();
                if (null != userEpage) {
                    netList = userEpage.getProduct();
                    Log.d(TAG, "######userEpage.getTotalCount()#####"+userEpage.getTotalCount());
                    if (null != netList && netList.size() > 0) {
                        epaperInfoList=netList;
                        bookAdapter = new BookAdapter(EditBookActivity.this, epaperInfoList,(int)bookWidth,(int)bookHeight);
                        gvBook.setAdapter(bookAdapter);
                    }
                }
            }
        });

    }

}
